﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;

namespace Arthasliu.Controls
{
    public partial class TitledTextBox : UserControl
    {
        public TitledTextBox()
        {
            InitializeComponent();

            contextTextBox.GotFocus += new EventHandler(contextTextBox_GotFocus);
        }

        void contextTextBox_GotFocus(object sender, EventArgs e)
        {
            var myTextBox = sender as TextBox;

            if (myTextBox==null)
            {
                return;
            }

            myTextBox.SelectAll();
        }

        public CheckBox TitleCheckBox
        {
            get
            {
                return titleCheckBox;
            }
        }

        public Label TitleLabel
        {
            get 
            { 
                return titleLabel; 
            }
        }

        public TextBox ContextTextBox
        {
            get
            {
                return contextTextBox;
            }
        }

        //[DefaultValue(false)]
        public bool CheckBox
        {
            get
            {
                return titleCheckBox.Visible;
            }
            set
            {
                titleCheckBox.Visible = value;
            }
        }

        [DefaultValue(false)]
        public bool Checked
        {
            get
            {
                return titleCheckBox.Checked;
            }
            set
            {
                titleCheckBox.Checked = value;
            }
        }

        public bool ContextReadOnly
        {
            get
            {
                return contextTextBox.ReadOnly;
            }
            set
            {
                contextTextBox.ReadOnly = value;
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        public AutoCompleteStringCollection AutoCompleteDataSource
        {
            get
            {
                return contextTextBox.AutoCompleteCustomSource;
            }
        }

        public string Title
        {
            get
            {
                return titleLabel.Text;
            }
            set
            {
                titleLabel.Text = value;
            }
        }

        public string Context
        {
            get
            {
                if (ContextSeparator==null||ContextSeparator == string.Empty)
                {

                    return contextTextBox.Text;
                }

                int mySepIndex = 0;
                mySepIndex = contextTextBox.Text.IndexOf(ContextSeparator);

                return contextTextBox.Text.Substring(mySepIndex);
            }
            set
            {
                contextTextBox.Text = value;
            }
        }

        public string ContextSeparator
        {
            get;
            set;
        }

        public delegate void ContextTextChangedHandler(object sender, EventArgs e);

        public event ContextTextChangedHandler ContextTextChanged;

        private void contextTextBox_TextChanged(object sender, EventArgs e)
        {
            if (ContextTextChanged!=null)
            {
                ContextTextChanged(sender, e);
            }
        }
    }
}
